package com.mdd.front.controller;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import com.mdd.front.validate.user.*;
import com.mdd.front.vo.user.*;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.mdd.common.aop.NotLogin;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.entity.user.User;
import com.mdd.common.util.StringUtils;
import com.mdd.front.LikeFrontThreadLocal;
import com.mdd.front.service.IUserService;
import com.mdd.front.vo.login.LoginTokenVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("api/user")
@Api(tags = "用户管理")
public class UserController {

    @Resource
    IUserService iUserService;

    @PostMapping("/realNameAuthentication")
    @ApiOperation(value="实名认证")
    public AjaxResult<Object> realNameAuthentication(@RequestBody User user) {
        Integer userId = LikeFrontThreadLocal.getUserId();
        Assert.notNull(user, "请上传资料");
        if(StringUtils.isEmpty(user.getRealName())
        		|| StringUtils.isEmpty(user.getIdCard()) 
        		|| StringUtils.isEmpty(user.getFrontView())) {
        	return AjaxResult.failed("请上传资料");
        }
        user.setId(userId);
        return iUserService.realNameAuthentication(user);
    }
    
    @GetMapping("/getAuthenticationInfo")
    @ApiOperation(value="获取用户实名认证资料")
    public AjaxResult<UserAuthVo> getAuthenticationInfo() {
    	Integer userId = LikeFrontThreadLocal.getUserId();
        return iUserService.getAuthenticationInfo(userId);
    }
    
    
    
    @NotLogin
    @GetMapping("/getUserEmUsername")
    @ApiOperation(value="获取环信用户ID")
    public AjaxResult<UserEmVo> getUserEmUsername(Integer userId) {
    	Assert.notNull(userId, "用户不存在");
        return iUserService.getUserEmUsername(userId);
    }
    
    
    @GetMapping("/center")
    @ApiOperation(value="个人中心")
    public AjaxResult<UserCenterVo> center() {
        Integer userId = LikeFrontThreadLocal.getUserId();
        Integer terminal = LikeFrontThreadLocal.getTerminal();

        UserCenterVo vo = iUserService.center(userId, terminal);
        return AjaxResult.success(vo);
    }

    @GetMapping("/info")
    @ApiOperation(value="个人信息")
    public AjaxResult<UserInfoVo> info() {
        Integer userId = LikeFrontThreadLocal.getUserId();
        UserInfoVo vo = iUserService.info(userId);
        return AjaxResult.success(vo);
    }

//    @PostMapping("/edit")
//    @ApiOperation(value="编辑信息")
//    public AjaxResult<Object> edit(@Validated @RequestBody UserUpdateValidate updateValidate) {
//        Integer userId = LikeFrontThreadLocal.getUserId();
//
//        iUserService.edit(updateValidate, userId);
//        return AjaxResult.success();
//    }
    @PostMapping("/edit")
    @ApiOperation(value="编辑信息")
    public AjaxResult<Object> edit(@RequestBody User user) {
        Integer userId = LikeFrontThreadLocal.getUserId();

        iUserService.edit(user, userId);
        return AjaxResult.success();
    }

    @PostMapping("/changePwd")
    @ApiOperation(value="修改密码")
    public AjaxResult<Object> changePwd(@Validated @RequestBody UserChangePwdValidate passwordValidate) {
        Integer userId = LikeFrontThreadLocal.getUserId();

        iUserService.changePwd(passwordValidate.getPassword(), passwordValidate.getOldPassword(), userId);
        return AjaxResult.success();
    }

    @NotLogin
    @PostMapping("/forgotPwd")
    @ApiOperation(value="忘记密码")
    public AjaxResult<Object> forgotPwd(@Validated @RequestBody UserForgetPwdValidate userForgetPwdValidate) {
        String password = userForgetPwdValidate.getPassword();
        String mobile = userForgetPwdValidate.getMobile();
        String code = userForgetPwdValidate.getCode();

        iUserService.forgotPwd(password, mobile, code);
        return AjaxResult.success();
    }

    @PostMapping("/bindMobile")
    @ApiOperation(value="绑定手机")
    public AjaxResult<UserBindVo> bindMobile(@Validated @RequestBody UserPhoneBindValidate mobileValidate) {
        Integer userId = LikeFrontThreadLocal.getUserId();
        System.out.println("进来bindMobile");
        UserBindVo u = iUserService.bindMobile(mobileValidate, userId);
        return AjaxResult.success(u);
    }

    @NotLogin
    @PostMapping("/mnpMobile")
    @ApiOperation(value="微信手机号")
    public AjaxResult<LoginTokenVo> mnpMobile(@Validated @RequestBody UserPhoneMnpValidate mobileValidate) {
    	System.out.println("进来mnpMobile");
        return AjaxResult.success(iUserService.mnpMobile(mobileValidate.getCode().trim()));
    }

    @PostMapping("/updateUser")
    @ApiOperation(value="更新新用户信息")
    public AjaxResult<Object> updateData(@Validated @RequestBody NewUserUpdateValidate newUserUpdateValidate) {
        Integer userId = LikeFrontThreadLocal.getUserId();
        iUserService.updateNewUserInfo(newUserUpdateValidate, userId);
        return AjaxResult.success();
    }

    @PostMapping("/bindMnp")
    @ApiOperation(value="绑定小程序")
    public AjaxResult<Object> bindMnp(@Validated @RequestBody UserBindWechatValidate BindMnpValidate) {
        Integer userId = LikeFrontThreadLocal.getUserId();
        iUserService.bindMnp(BindMnpValidate, userId);
        System.out.println("进来bindMnp");
        return AjaxResult.success();
    }
    
    @PostMapping("/bindAppWechat")
    @ApiOperation(value="绑定APP")
    public AjaxResult<Object> bindAppWechat(@Validated @RequestBody UserBindWechatValidate BindAppValidate) {
        Integer userId = LikeFrontThreadLocal.getUserId();
        iUserService.bindAppWechat(BindAppValidate, userId);
        System.out.println("进来bindAppWechat");
        return AjaxResult.success();
    }

    @PostMapping("/bindOa")
    @ApiOperation(value="绑定微信公众号")
    public AjaxResult<Object> bindOa(@Validated @RequestBody UserBindWechatValidate BindOaValidate) {
        Integer userId = LikeFrontThreadLocal.getUserId();
        iUserService.bindOa(BindOaValidate, userId);
        return AjaxResult.success();
    }

    @GetMapping("/cancellation")
    @ApiOperation(value="绑定微信公众号")
    public AjaxResult<Object> cancellation() {
        Integer userId = LikeFrontThreadLocal.getUserId();
        List<Map<String, Object>> cancellation = iUserService.cancellation(userId);
        return AjaxResult.success(cancellation);
    }
    
    @GetMapping("/qryMsgNotify")
    @ApiOperation(value="查询消息通知开关")
    public AjaxResult<Object> qryMsgNotify() {
        Integer userId = LikeFrontThreadLocal.getUserId();
        return iUserService.qryMsgNotify(userId);
    }

    
    @PostMapping("/operateMsgNotify")
    @ApiOperation(value="操作消息通知")
    public AjaxResult<Object> operateMsgNotify() {
        Integer userId = LikeFrontThreadLocal.getUserId();
        return iUserService.operateMsgNotify(userId);
    }


    @PostMapping("/updateUserPhone")
    @ApiOperation(value="更新新用户手机号码")
    public AjaxResult<Object> updateUserPhone(@Validated @RequestBody UserMobileUpdateValidate userMobileUpdateValidate) {
        String mobile = userMobileUpdateValidate.getMobile();
        String mobileCode = userMobileUpdateValidate.getMobileCode();
        Integer userId = LikeFrontThreadLocal.getUserId();
        iUserService.updateUserPhoneInfo(mobile,mobileCode, userId);
        return AjaxResult.success();
    }

}
